using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using ENTITIES;

namespace DAL
{
  public  class CTDonHangDAL
    {
      
        private SqlConnection cn = null;
        public CTDonHangDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
       public void Insert (TBCTDonHang ctdh)
        {
            SqlCommand cm = new SqlCommand("CTDonHang_insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@SoDH",ctdh.sodh);
            cm.Parameters.AddWithValue("@MaSP", ctdh.masp);
            cm.Parameters.AddWithValue("@GiaBan", ctdh.giaban);
            cm.Parameters.AddWithValue("@SoLuong", ctdh.soluong);
           
            cm.ExecuteNonQuery();
        }
        public void Update(TBCTDonHang ctdh)
        {
            SqlCommand cm = new SqlCommand("CTDonHang_update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaCTDH", ctdh.mactdh);
            cm.Parameters.AddWithValue("@SoDH", ctdh.sodh);
            cm.Parameters.AddWithValue("@MaSP", ctdh.masp);
            cm.Parameters.AddWithValue("@GiaBan", ctdh.giaban);
            cm.Parameters.AddWithValue("@SoLuong", ctdh.soluong);
          
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("CTDonHang_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaCTDH", iD);
            cm.ExecuteNonQuery();
        }

        public tbCTDonHangCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("CTDonHang_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbCTDonHangCollection ctdhCollection = new tbCTDonHangCollection();

            while (dr.Read())
            {
                TBCTDonHang ctdh = new TBCTDonHang();
                ctdh.mactdh = int.Parse(dr.GetValue(0).ToString());
                ctdh.sodh = int.Parse(dr.GetValue(1).ToString());
                ctdh.masp = int.Parse(dr.GetValue(2).ToString());
                ctdh.giaban = int.Parse(dr.GetValue(3).ToString());
                ctdh.soluong = int.Parse(dr.GetValue(4).ToString());
             
                ctdhCollection.Add(ctdh);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return ctdhCollection;
        }
    }
}
    

